package com.lej.home

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.lej.core.base.BaseModel
import com.lej.core.base.BaseViewModel
import com.lej.core.utils.extention.safe
import com.lej.dataservice.club.bean.ApiPageResult
import com.lej.dataservice.club.bean.Loading
import com.lej.dataservice.club.bean.error
import com.lej.dataservice.club.bean.main.my.MyUserInfoAll
import com.lej.dataservice.club.bean.success
import com.lej.dataservice.club.http.checkData
import com.lej.dataservice.club.http.checkList
import com.lej.dataservice.club.http.request
import com.lej.dataservice.club.http.requestMap
import com.lej.home.http.homeApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class HomeViewModel : BaseViewModel<BaseModel>() {
    val allList = MutableLiveData<ApiPageResult<Any>>()

    fun getListData(page: Int = 1) {
        viewModelScope.launch(Dispatchers.Main) {
            runCatching {
                allList.postValue(allList.value.Loading(page))
                val async1 = async {
                    runCatching { homeApiService.bannerHouseList().checkList() }.getOrNull()
                }
                val async2 = async { runCatching { homeApiService.NFTRoomList() }.getOrNull() }
                val bannerList = async1.await()
                val res = async2.await()
                val nftRoomList = res?.checkList().safe()
                val aList = ArrayList<Any>()
                if (bannerList != null) aList.add(bannerList)
                aList.addAll(nftRoomList)
                allList.postValue(allList.value.success(page, aList, true))
            }.onFailure {
                allList.postValue(allList.value.error(page, it))
            }
        }
    }

    fun getHomeData() = request { homeApiService.getHomeData() }
    fun getPersonInfo() = request { homeApiService.getPersonSpaceInfo() }

    suspend fun getUserInfo(): MyUserInfoAll? {
        kotlin.runCatching {
            return homeApiService.getUserInfo().checkData()
        }
        return null
    }

    suspend fun getActiveNotice(versionName: String): String? {
        kotlin.runCatching {
           return homeApiService.activeNotice(listOf("qixiActiveNotice"), versionName).checkData()
                .get("qixiActiveNotice").asString
        }
        return null
    }

    suspend fun getPCAddress(versionName: String): String? {
        showDialog()
        kotlin.runCatching {
            val address = homeApiService.activeNotice(listOf("pcAddress"), versionName).checkData()
                .get("pcAddress").asString
            dismissDialog()
            return address
        }
        dismissDialog()
        return null
    }

    //获取用户状态 1 邀请用户 2 非邀请用户
    fun getUserStatus() =
        requestMap(map = { get("attr").asInt == 1 }) { homeApiService.usersAttr() }

    fun getUnreadMsg() = requestMap(map = { get("num").asInt }) { homeApiService.getUnreadMsg() }
}